<?php
class User extends AppModel {
	var $name = 'User';
	var $validate = array(
		'username' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Username cannot be empty.',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'Between 5 and 15 chars'=>array(
				'rule'=>array('between',4,15),
				'message'=>'The username must be between 4 and 15 characters.'
			),
			'unique username' => array(
				'rule' => 'isUnique',
				'message' => 'That username has already been taken'),
		),

		'password' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
				'rule'=>array('between',4,15),
				'message'=>'The password must be between 4 and 15 characters.'
			),
			'the password match' => array(
				'rule'=> 'matchPasswords',
				'message' => 'Passwords do not match'
			),
			'Between 5 and 15 chars'=>array(
				'rule'=>array('between',4,15),
				'message'=>'The password must be between 4 and 15 characters.'
			),
		),
		'roles' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'email' => array(
				'allowEmpty' => true,
				'rule' =>'isUnique',
				'message' => 'That email is already in the system.'),
	);
	function matchPasswords($data) {
		if($data['password']==$this->data['User']['password_confirmation']) {
			return true;
		}
		$this->invalidate('password_confirmation','Password do not match');
		return false;
	}
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $hasMany = array(
		'Song' => array(
			'className' => 'Song',
			'foreignKey' => 'user_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);

	function beforeSave() {
		$this->hashPasswords(null, true);
		return true;
	}
	function hashPasswords($data) {
		if(isset($this->data['User']['password'])) {
			$this->data['User']['password'] = Security::hash($this->data['User']['password'], null, true);
			return $data;
		}
		return $data;
	}

}
